package com.cyt.gen.utils;

import com.cyt.gen.model.vo.GeneratorVo;
import com.cyt.gen.service.FormFieldService;
import com.cyt.gen.service.FormTableService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author rong yuan
 * @date 2019/7/4 11:56
 */
public class GeneratorGeneralUtil extends GeneratorFormUtil {

    public GeneratorGeneralUtil(GeneratorVo generatorVo, FormTableService formTableService, FormFieldService formFieldService) {
        super(generatorVo, formTableService, formFieldService);
        this.outRootJava = projectPath + "cyt-" + packageName + "/src/main/java/com/cyt/" + moduleName;
        this.templateRootJava = projectPath + "cyt-gen/src/main/resources/template/general/java/";
        this.outRootController = projectPath + "cyt-web/src/main/java/com/cyt/web/controller/" + moduleName;
        this.templateRootController = projectPath + "cyt-gen/src/main/resources/template/general/web/";
    }

    @Override
    public void generatorCode() throws Exception {
        super.generatorCode();
        //生成xml和html代码
        String templatePath = projectPath + "cyt-gen/src/main/resources/template/general/";
        List<String> templateFiles = getTemplateFiles();
        List<String> outFiles = getOutFiles();
        Map<String, Object> map = getDataModel();
        generatorCodeList(outFiles, templateFiles, templatePath, map);
    }

    private List<String> getTemplateFiles() {
        List<String> templateFiles = new ArrayList<>();
        templateFiles.add("xml/${entity}Mapper.xml.ftl");
        templateFiles.add("html/${entityLowerCase}List.html");
        templateFiles.add("html/${entityLowerCase}Edit.html");
        return templateFiles;
    }

    private List<String> getOutFiles() {
        List<String> outFiles = new ArrayList<>();
        String basePath = projectPath + "cyt-" + packageName + "/src/main/resources/mapping/" + moduleName;
        outFiles.add(basePath + "/" + entityName + "Mapper.xml");
        basePath = projectPath + "cyt-web/src/main/webapp/WEB-INF/html/" + moduleName + "/" + entityName.replace(moduleName, "").toLowerCase();
        outFiles.add(basePath + "/" + Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1) + "List.html");
        outFiles.add(basePath + "/" + Character.toLowerCase(entityName.charAt(0)) + entityName.substring(1) + "Edit.html");
        return outFiles;
    }
}